


######################Preparation of datasets and loading of required libary: ALWAYS RUN THIS PART###################################


#Try determine which are really used...
Sys.setenv(lang = "en_US")
library(tidyverse) #installed
library(stargazer) #installed
library(fixest) #installed
library(modelsummary) #installed

library(marginaleffects)
library(xlsx)

rm(list=ls( )) #remove whatever was there before
#setwd("") #Change it to your own folder name.

load("fulldata.RData")

#rm(rawdemo1,rawipip1,rawraven1,rawriskav1,rawsess1,rawsess2,rawnep1,aggtime1,rawtime1)
df<-d
rm(d)
df$contrib_share<- ifelse(df$DCG_G_endowment>0,df$DCG_P_contribution/df$DCG_G_endowment ,0)
df$DCG_round_ingame= df$DCG_G_round-df$DCG_G_startround+1


#Some manual corrections (double checked with prolifc exports)
df$birthyear[df$participant.label=="61121de38e73c67c115b8459"] <-1999
df$birthyear[df$participant.label=="61367272e2bdffeec554136f"] <-2022-22
df$birthyear[df$participant.label=="6106c0aecdc0c01341f2bf2d"] <-2022-34
df$birthyear[df$participant.label=="5f312d34c38e2c0742e3f38d"] <-2000

df$age=2022-df$birthyear

#Calculating some variables
df$n=ifelse(df$DCG_G_treatment==3,1,4)
df$DCG_G_threshmet = ifelse(df$DCG_G_total_contribution>= df$DCG_G_threshold,1,0)
df$DCG_G_sustainable=ifelse(df$DCG_G_endonext==100,1,0)
df$DCG_G_waste=ifelse(df$DCG_G_threshmet==1,df$DCG_G_total_contribution - df$DCG_G_threshold,0)
df$DCG_G_wastebyn=ifelse(df$DCG_G_treatment==3,df$DCG_G_waste,df$DCG_G_waste/4)
df$DCG_G_waste_pc_threshold=df$DCG_G_waste/df$DCG_G_threshold

df$DCG_G_waste_a=ifelse(df$DCG_G_threshmet==1,df$DCG_G_total_contribution - df$DCG_G_threshold,NA)
df$DCG_G_wastebyn_a=ifelse(df$DCG_G_treatment==3 ,df$DCG_G_waste_a,df$DCG_G_waste_a/4)
df$DCG_P_overcontrib = pmax(df$DCG_P_contribution - df$DCG_G_threshold/df$n,0)
df$discounted_pay = df$DCG_P_private_account*(df$DCG_G_continuation_probability/100)^(df$DCG_round_ingame-1)

df$DCG_G_wastedum=ifelse(df$DCG_G_waste>0,1,0)
df$DCG_P_overcontribdum=ifelse(df$DCG_P_overcontrib>0,1,0)

#df$DCG_G_d <- ifelse(df$DCG_G_treatment==4 |df$DCG_G_treatment==5 ,0.5 ,0.25)

df$diff_to_eq_coop=abs(df$contrib_share - df$DCG_G_d)
df$close_to_eq_coop=ifelse(df$diff_to_eq_coop<=0.02,1,0)
df$close_to_eq_selfish=ifelse(df$contrib_share<=0.02,1,0)

inbet=(1-df$DCG_G_continuation_probability/100*(1-df$DCG_G_g*(df$DCG_G_d*df$n-1)))/(df$DCG_G_continuation_probability/100*df$DCG_G_g*(df$n-1))
df$InBetween=ifelse(inbet<= df$DCG_G_d & inbet >=0,inbet,NA)


df$close_to_inbetween=ifelse(df$DCG_G_treatment!=2,NA,ifelse(abs(df$contrib_share - df$InBetween)<=0.02,1,0))

df$Treatment_name=ifelse(df$DCG_G_treatment==1,"T-Baseline",ifelse(df$DCG_G_treatment==2,"T-LowThreshold","T-OnePlayer"))

df$female=ifelse(df$gender==1,1,0)

#complete DCG only: The dataset dc only keeps groups with complete data, that is excludes groups where someone dropped out.
#vector of groups with dropouts:
unique(df$UniqGroupID[df$DCG_G_dropout_in_group==1]) #11 groups
#D2 = subset(D1, !(V1 %in% c('B','N','T')))
dc<-subset(df, !(UniqGroupID %in% unique(df$UniqGroupID[df$DCG_G_dropout_in_group==1])))
#manually remove the one participant hwo dropped out in T3
dc<-dc[dc$participant.label!="5e8ed13b0bf7560008002d52",]
#Check dc has complete data (50 first columns are what we start with, before calculations):
any(is.na(dc[,c(1:50)]))


#subsets/aggregates of interest
# d only keeps the first 5 rounds of each repetition. d1 only takes round 1 of each repetition
# da takes the average of the 5 periods of the game (i.e. 1 observation per repetition for each participant) 
# dg takes average values by group (1 observation per group for each period of each repet)
# dga takes the average of the five periods for each group (1 observation per repetition for each group)
d<-dc[dc$DCG_round_ingame<=5,]
da<- aggregate(d, by=list(d$UniqParticipantID, d$DCG_G_repet), mean)
dg<- aggregate(d, by=list(d$UniqGroupID, d$DCG_G_round), mean)
dg$Treatment_name=ifelse(dg$DCG_G_treatment==1,"T-Baseline",ifelse(dg$DCG_G_treatment==2,"T-LowThreshold","T-OnePlayer"))
dga<- aggregate(dg, by=list(dg$UniqGroupID, dg$DCG_G_repet), mean)
d1=d[d$DCG_G_round==1,]

length(unique(dc$UniqGroupID[dc$DCG_G_treatment==1]))
length(unique(dc$UniqGroupID[dc$DCG_G_treatment==2]))
length(unique(dc$UniqGroupID[dc$DCG_G_treatment==3]))


########################################End of Preparation###################################################



########################################## Summary statistics table#################################################
dsum<- d%>%select( DCG_P_contribution,contrib_share, DCG_P_private_account,close_to_eq_coop, close_to_eq_selfish, close_to_inbetween )
stargazer(dsum, omit.summary.stat=c("p25", "p75"))
#stargazer(dsum, omit.summary.stat=c("p25", "p75"), out="summstat.tex")

#For personal charac => only count 1 per person (not 1 for every round...)
dsum1<-d1%>%select(ipip_A, ipip_C, female, age, raven,riskaversion_contrib,nep,co2 )
stargazer(dsum1, omit.summary.stat=c("p25", "p75"), type="text")




#######################FOR PAPER: Aggregate Plots #####################################################################
####Export as pdf, choose "device size" and adjust manually to 8*8, landscape. 

#Redo plots to make them B/W comptible:

#FIGURE 2 - close to coop, close to defect, threshold met

#close to cooperation
ggplot(dc[dc$DCG_round_ingame <=5,], aes(x=DCG_round_ingame, y=close_to_eq_coop,shape=factor(Treatment_name),color=factor(Treatment_name))) +
  geom_line(size = 1, stat = "summary", fun = "mean", show.legend = FALSE )+
  stat_summary(fun.data = "mean_cl_normal", geom = "pointrange",size=1.1)+
  labs(title="Share of participants who played Close to Cooperation", color = "",shape="", x="Period", y="Close to Cooperation") + coord_cartesian(xlim =c(1, 5), ylim = c(0, 0.8))+
  theme_bw()+ theme(legend.position="bottom")+theme(legend.spacing.x = unit(0.5, 'cm'),legend.text=element_text(size=rel(1.5)))+
  theme(axis.title.x=element_text(size=rel(1.5)) ,axis.title.y=element_text(size=rel(1.5)), plot.title = element_text(size=rel(1.7)), axis.text =element_text(size=rel(1.5)) )


#close to defection
ggplot(dc[dc$DCG_round_ingame <=5,], aes(x=DCG_round_ingame, y=close_to_eq_selfish,shape=factor(Treatment_name),color=factor(Treatment_name))) +
  geom_line(size = 1, stat = "summary", fun = "mean", show.legend = FALSE )+
  stat_summary(fun.data = "mean_cl_normal", geom = "pointrange",size=1.1)+
  labs(title="Share of participants who played Close to Defection", color = "",shape="", x="Period", y="Close to Defection") + coord_cartesian(xlim =c(1, 5), ylim = c(0, 0.8))+
  theme_bw()+ theme(legend.position="bottom")+theme(legend.spacing.x = unit(0.5, 'cm'),legend.text=element_text(size=rel(1.5)))+
  theme(axis.title.x=element_text(size=rel(1.5)) ,axis.title.y=element_text(size=rel(1.5)), plot.title = element_text(size=rel(1.7)), axis.text =element_text(size=rel(1.5)) )

#threshmet
ggplot(dg, aes(x=DCG_round_ingame, y=DCG_G_threshmet,shape=factor(Treatment_name),color=factor(Treatment_name))) +
  geom_line(size = 1, stat = "summary", fun = "mean", show.legend = FALSE )+
  stat_summary(fun.data = "mean_cl_normal", geom = "pointrange",size=1.1)+
  labs(title="Share of groups who met the threshold", color = "",shape="", x="Period", y="Threshold Met") + coord_cartesian(xlim =c(1, 5), ylim = c(0, 0.8))+
  theme_bw()+ theme(legend.position="bottom")+theme(legend.spacing.x = unit(0.5, 'cm'),legend.text=element_text(size=rel(1.5)))+
  theme(axis.title.x=element_text(size=rel(1.5)) ,axis.title.y=element_text(size=rel(1.5)), plot.title = element_text(size=rel(1.7)), axis.text =element_text(size=rel(1.5)) )



#FIGURE 4: average endowment and sustainability evol
ggplot(dg, aes(x=DCG_round_ingame, y=DCG_G_endowment,shape=factor(Treatment_name),color=factor(Treatment_name))) +
  geom_line(size = 1, stat = "summary", fun = "mean", show.legend = FALSE )+
  stat_summary(fun.data = "mean_cl_normal", geom = "pointrange",size=1.1)+
  labs(title="Average endowment", color = "",shape="", x="Period", y="Endowment") + coord_cartesian(xlim =c(1, 5), ylim = c(0, 100))+
  theme_bw()+ theme(legend.position="bottom")+theme(legend.spacing.x = unit(0.5, 'cm'),legend.text=element_text(size=rel(1.5)))+
  theme(axis.title.x=element_text(size=rel(1.5)) ,axis.title.y=element_text(size=rel(1.5)), plot.title = element_text(size=rel(1.7)), axis.text =element_text(size=rel(1.5)) )

ggplot(dg, aes(x=DCG_round_ingame, y=DCG_G_sustainable,shape=factor(Treatment_name),color=factor(Treatment_name))) +
  geom_line(size = 1, stat = "summary", fun = "mean", show.legend = FALSE )+
  stat_summary(fun.data = "mean_cl_normal", geom = "pointrange",size=1.1)+
  labs(title="Share of sustainable groups", color = "",shape="", x="Period", y="Sustainable") + coord_cartesian(xlim =c(1, 5), ylim = c(0, 0.8))+
  theme_bw()+ theme(legend.position="bottom")+theme(legend.spacing.x = unit(0.5, 'cm'),legend.text=element_text(size=rel(1.5)))+
  theme(axis.title.x=element_text(size=rel(1.5)) ,axis.title.y=element_text(size=rel(1.5)), plot.title = element_text(size=rel(1.7)), axis.text =element_text(size=rel(1.5)) )


#FIGURE 5 - learning effects
#close to cooperation
ggplot(dc[dc$DCG_round_ingame <=5,], aes(x=DCG_G_repet, y=close_to_eq_coop,shape=factor(Treatment_name),color=factor(Treatment_name))) +
  geom_line(size = 1, stat = "summary", fun = "mean", show.legend = FALSE )+
  stat_summary(fun.data = "mean_cl_normal", geom = "pointrange",size=1.1)+
  labs(title="Share of participants who played Close to Cooperation", color = "",shape="", x="Period", y="Close to Cooperation") + coord_cartesian(xlim =c(1, 5), ylim = c(0, 0.8))+
  theme_bw()+ theme(legend.position="bottom")+theme(legend.spacing.x = unit(0.5, 'cm'),legend.text=element_text(size=rel(1.5)))+
  theme(axis.title.x=element_text(size=rel(1.5)) ,axis.title.y=element_text(size=rel(1.5)), plot.title = element_text(size=rel(1.7)), axis.text =element_text(size=rel(1.5)) )


#close to defection
ggplot(dc[dc$DCG_round_ingame <=5,], aes(x=DCG_G_repet, y=close_to_eq_selfish,shape=factor(Treatment_name),color=factor(Treatment_name))) +
  geom_line(size = 1, stat = "summary", fun = "mean", show.legend = FALSE )+
  stat_summary(fun.data = "mean_cl_normal", geom = "pointrange",size=1.1)+
  labs(title="Share of participants who played Close to Defection", color = "",shape="", x="Period", y="Close to Defection") + coord_cartesian(xlim =c(1, 5), ylim = c(0, 0.8))+
  theme_bw()+ theme(legend.position="bottom")+theme(legend.spacing.x = unit(0.5, 'cm'),legend.text=element_text(size=rel(1.5)))+
  theme(axis.title.x=element_text(size=rel(1.5)) ,axis.title.y=element_text(size=rel(1.5)), plot.title = element_text(size=rel(1.7)), axis.text =element_text(size=rel(1.5)) )

#threshmet
ggplot(dg, aes(xDCG_G_repet, y=DCG_G_threshmet,shape=factor(Treatment_name),color=factor(Treatment_name))) +
  geom_line(size = 1, stat = "summary", fun = "mean", show.legend = FALSE )+
  stat_summary(fun.data = "mean_cl_normal", geom = "pointrange",size=1.1)+
  labs(title="Share of groups who met the threshold", color = "",shape="", x="Period", y="Threshold Met") + coord_cartesian(xlim =c(1, 5), ylim = c(0, 0.8))+
  theme_bw()+ theme(legend.position="bottom")+theme(legend.spacing.x = unit(0.5, 'cm'),legend.text=element_text(size=rel(1.5)))+
  theme(axis.title.x=element_text(size=rel(1.5)) ,axis.title.y=element_text(size=rel(1.5)), plot.title = element_text(size=rel(1.7)), axis.text =element_text(size=rel(1.5)) )

ggplot(dc[dc$DCG_round_ingame <=5,], aes(x=DCG_G_repet, y=contrib_share,shape=factor(Treatment_name),color=factor(Treatment_name))) +
  geom_line(size = 1, stat = "summary", fun = "mean", show.legend = FALSE )+
  stat_summary(fun.data = "mean_cl_normal", geom = "pointrange",size=1.1)+
  labs(title="Average contribution share to the public account", color = "",shape="", x="Period", y="Contribution share") + coord_cartesian(xlim =c(1, 5), ylim = c(0, 0.6))+
  theme_bw()+ theme(legend.position="bottom")+theme(legend.spacing.x = unit(0.5, 'cm'),legend.text=element_text(size=rel(1.5)))+
  theme(axis.title.x=element_text(size=rel(1.5)) ,axis.title.y=element_text(size=rel(1.5)), plot.title = element_text(size=rel(1.7)), axis.text =element_text(size=rel(1.5)) )


#FIGURE B.2 contribution to public account (level and shares)

ggplot(dc[dc$DCG_round_ingame <=5,], aes(x=DCG_round_ingame, y=DCG_P_contribution,shape=factor(Treatment_name),color=factor(Treatment_name))) +
  geom_line(size = 1, stat = "summary", fun = "mean", show.legend = FALSE )+
  stat_summary(fun.data = "mean_cl_normal", geom = "pointrange",size=1.1)+
  labs(title="Average contribution level to the public account", color = "",shape="", x="Period", y="Contribution") + coord_cartesian(xlim =c(1, 5), ylim = c(0, 60))+
  theme_bw()+ theme(legend.position="bottom")+theme(legend.spacing.x = unit(0.5, 'cm'),legend.text=element_text(size=rel(1.5)))+
  theme(axis.title.x=element_text(size=rel(1.5)) ,axis.title.y=element_text(size=rel(1.5)), plot.title = element_text(size=rel(1.7)), axis.text =element_text(size=rel(1.5)) )

ggplot(dc[dc$DCG_round_ingame <=5,], aes(x=DCG_round_ingame, y=contrib_share,shape=factor(Treatment_name),color=factor(Treatment_name))) +
  geom_line(size = 1, stat = "summary", fun = "mean", show.legend = FALSE )+
  stat_summary(fun.data = "mean_cl_normal", geom = "pointrange",size=1.1)+
  labs(title="Average contribution share to the public account", color = "",shape="", x="Period", y="Contribution share") + coord_cartesian(xlim =c(1, 5), ylim = c(0, 0.6))+
  theme_bw()+ theme(legend.position="bottom")+theme(legend.spacing.x = unit(0.5, 'cm'),legend.text=element_text(size=rel(1.5)))+
  theme(axis.title.x=element_text(size=rel(1.5)) ,axis.title.y=element_text(size=rel(1.5)), plot.title = element_text(size=rel(1.7)), axis.text =element_text(size=rel(1.5)) )





#########################PLOTS based on regression models ########################

#FIGURE 3
mod301<-feglm(close_to_eq_coop ~ factor(Treatment_name), data=d[d$DCG_round_ingame==1,],  cluster=~UniqGroupID , family ="binomial")
mod302<-feglm(close_to_eq_coop ~ factor(Treatment_name), data=d[d$DCG_round_ingame==2,],  cluster=~UniqGroupID , family ="binomial")
mod303<-feglm(close_to_eq_coop ~ factor(Treatment_name), data=d[d$DCG_round_ingame==3,],  cluster=~UniqGroupID , family ="binomial")
mod304<-feglm(close_to_eq_coop ~ factor(Treatment_name), data=d[d$DCG_round_ingame==4,],  cluster=~UniqGroupID , family ="binomial")
mod305<-feglm(close_to_eq_coop ~ factor(Treatment_name), data=d[d$DCG_round_ingame==5,],  cluster=~UniqGroupID , family ="binomial")



dpl<-modelplot(list(mod301, mod302, mod303, mod304, mod305), coef_omit ="(Intercept)", draw=FALSE)
dpl$Period=str_sub(dpl$model,-1)
dpl$term <- gsub("factor\\(Treatment_name)","", dpl$term)


ggplot(dpl, aes(y=estimate, x=Period,color=term, shape=term , ymin=conf.low, ymax=conf.high ))+
  geom_pointrange(position=position_dodge(width = 0.3), size=1.1)+theme_bw()+geom_hline(yintercept=0)+ 
  labs(title="Treatment effects on Close to Cooperation", color = "",shape="", x="Period", y="Regression estimate") +
  theme(legend.position="bottom") +theme(legend.spacing.x = unit(0.5, 'cm'),legend.text=element_text(size=rel(1.5)))+
  theme(axis.title.x=element_text(size=rel(1.5)) ,axis.title.y=element_text(size=rel(1.5)), plot.title = element_text(size=rel(1.7)), axis.text =element_text(size=rel(1.5)) )



mod401<-feglm(close_to_eq_selfish ~ factor(Treatment_name), data=d[d$DCG_round_ingame==1,],  cluster=~UniqGroupID , family ="binomial")
mod402<-feglm(close_to_eq_selfish ~ factor(Treatment_name), data=d[d$DCG_round_ingame==2,],  cluster=~UniqGroupID , family ="binomial")
mod403<-feglm(close_to_eq_selfish ~ factor(Treatment_name), data=d[d$DCG_round_ingame==3,],  cluster=~UniqGroupID , family ="binomial")
mod404<-feglm(close_to_eq_selfish ~ factor(Treatment_name), data=d[d$DCG_round_ingame==4,],  cluster=~UniqGroupID , family ="binomial")
mod405<-feglm(close_to_eq_selfish ~ factor(Treatment_name), data=d[d$DCG_round_ingame==5,],  cluster=~UniqGroupID , family ="binomial")




dpl<-modelplot(list(mod401, mod402, mod403, mod404, mod405), coef_omit ="(Intercept)", draw=FALSE)
dpl$Period=str_sub(dpl$model,-1)
dpl$term <- gsub("factor\\(Treatment_name)","", dpl$term)


ggplot(dpl, aes(y=estimate, x=Period,color=term ,shape=term, ymin=conf.low, ymax=conf.high ))+
  geom_pointrange(position=position_dodge(width = 0.3), size=1.1)+theme_bw()+geom_hline(yintercept=0)+ 
  labs(title="Treatment effects on Close to Defection", color = "", shape="",x="Period", y="Regression estimate") +
  theme(legend.position="bottom") +theme(legend.spacing.x = unit(0.5, 'cm'),legend.text=element_text(size=rel(1.5)))+
  theme(axis.title.x=element_text(size=rel(1.5)) ,axis.title.y=element_text(size=rel(1.5)), plot.title = element_text(size=rel(1.7)), axis.text =element_text(size=rel(1.5)) )




#threshold met
mod601<-feglm(DCG_G_threshmet~ factor(Treatment_name), data=dg[dg$DCG_round_ingame==1,],  cluster=~UniqGroupID, family="binomial")
mod602<-feglm(DCG_G_threshmet~ factor(Treatment_name), data=dg[dg$DCG_round_ingame==2,],  cluster=~UniqGroupID, family="binomial")
mod603<-feglm(DCG_G_threshmet~ factor(Treatment_name), data=dg[dg$DCG_round_ingame==3,],  cluster=~UniqGroupID, family="binomial")
mod604<-feglm(DCG_G_threshmet~ factor(Treatment_name), data=dg[dg$DCG_round_ingame==4,],  cluster=~UniqGroupID, family="binomial")
mod605<-feglm(DCG_G_threshmet~ factor(Treatment_name), data=dg[dg$DCG_round_ingame==5,],  cluster=~UniqGroupID, family="binomial")



dpl<-modelplot(list(mod601, mod602, mod603, mod604, mod605), coef_omit ="(Intercept)", draw=FALSE)
dpl$Period=str_sub(dpl$model,-1)
dpl$term <- gsub("factor\\(Treatment_name)","", dpl$term)

ggplot(dpl, aes(y=estimate, x=Period,color=term ,shape=term, ymin=conf.low, ymax=conf.high ))+
  geom_pointrange(position=position_dodge(width = 0.3), size=1.1)+theme_bw()+geom_hline(yintercept=0)+ 
  labs(title="Treatment effects on Threshold met", color = "",shape="", x="Period", y="Regression estimate") +
  theme(legend.position="bottom") +theme(legend.spacing.x = unit(0.5, 'cm'),legend.text=element_text(size=rel(1.5)))+
  theme(axis.title.x=element_text(size=rel(1.5)) ,axis.title.y=element_text(size=rel(1.5)), plot.title = element_text(size=rel(1.7)), axis.text =element_text(size=rel(1.5)) )







#############################FOR PAPER: regressions and tex tables using feglm, feols#######################

#Need to run the dict2 command first! it is necessary to have it defined before running the other regressions.
dict2<-c( "(Intercept)" = "Constant", "factor(DCG_G_treatment)2" = "T-LowThreshold", "factor(DCG_G_treatment)3" = "T-OnePlayer",
          gender="Female", age="Age", raven="Raven", riskaversion_contrib = "Risk Tolerance", ipip_A="Agreeableness", ipip_C="Conscientiousness",
          close_to_eq_coop="Close to Cooperation", close_to_eq_selfish ="Close to Defection", UniqGroupID="Group", DCG_round_ingame="Period", 
          DCG_G_endowment = "Endowment", contrib_share="Contribution Share", Treatmentdummies="Treatment dummies")

#Table 4
m1<-feglm(close_to_eq_coop ~ factor(DCG_G_treatment), data=d[d$DCG_round_ingame==1,], family ="binomial",cluster=~UniqGroupID)
m2<-feglm(close_to_eq_coop ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=d[d$DCG_round_ingame==1,], family ="binomial",cluster=~UniqGroupID)
m3<-feglm(close_to_eq_coop ~ factor(DCG_G_treatment), data=d[d$DCG_round_ingame==5,], family ="binomial",cluster=~UniqGroupID)
m4<-feglm(close_to_eq_coop ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=d[d$DCG_round_ingame==5,], family ="binomial",cluster=~UniqGroupID)
m5<-feols(close_to_eq_coop ~ factor(DCG_G_treatment), data=da,cluster=~UniqGroupID)
m6<-feols(close_to_eq_coop ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=da,cluster=~UniqGroupID)

etable(m1,m2,m3,m4,m5,m6, headers = c("period 1","period 1", "period 5","period 5", "average 1-5","average 1-5"), tex=TRUE, file="Tab_Paper.tex", 
       title="Treatment effects on playing Close to Cooperation", label="tab:TreatCoop", fitstat=c('n', 'r2', 'pr2', 'wald'), dict=dict2, order=c("T-One","!Constant"), 
       drop=c("%ipip", "%female", "%age", "%raven" , "%riskaversion_contrib"), extralines= list(Controls=c( "No", "Yes", "No", "Yes", "No", "Yes" ) ))

#Calculate marginal effects:
ame_m1 <- avg_slopes(m1)
ame_m1$model="period 1 - no controls"
ame_m2<- avg_slopes(m2, variables = c("DCG_G_treatment"))
ame_m2$model="period 1 - with controls"
ame_m3 <- avg_slopes(m3)
ame_m3$model = "period 5 - no controls"
ame_m4<- avg_slopes(m4, variables = c("DCG_G_treatment"))
ame_m4$model = "period 5 - with controls"
ame_closecoop <- rbind(ame_m1, ame_m2, ame_m3, ame_m4)
ame_closecoop$term <- ifelse(ame_closecoop$contrast=="2 - 1","T-LowThreshold","T-OnePlayer")
ame_closecoop <- ame_closecoop %>% select(term,estimate,std.error,p.value,model)

#Table 5
m1<-feglm(close_to_eq_selfish ~ factor(DCG_G_treatment), data=d[d$DCG_round_ingame==1,], family ="binomial",cluster=~UniqGroupID)
m2<-feglm(close_to_eq_selfish ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=d[d$DCG_round_ingame==1,], family ="binomial",cluster=~UniqGroupID)
m3<-feglm(close_to_eq_selfish ~ factor(DCG_G_treatment), data=d[d$DCG_round_ingame==5,], family ="binomial",cluster=~UniqGroupID)
m4<-feglm(close_to_eq_selfish ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=d[d$DCG_round_ingame==5,], family ="binomial",cluster=~UniqGroupID)
m5<-feols(close_to_eq_selfish ~ factor(DCG_G_treatment), data=da,cluster=~UniqGroupID)
m6<-feols(close_to_eq_selfish ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=da,cluster=~UniqGroupID)

etable(m1,m2,m3,m4,m5,m6, headers = c("period 1","period 1", "period 5","period 5", "average 1-5","average 1-5"), tex=TRUE, file="Tab_Paper.tex", 
       title="Treatment effects on playing Close to Defection", label="tab:TreatDefect", fitstat=c('n', 'r2', 'pr2', 'wald'), dict=dict2, order=c("T-One","!Constant"), 
       drop=c("%ipip", "%female", "%age", "%raven" , "%riskaversion_contrib"), extralines= list(Controls=c( "No", "Yes", "No", "Yes", "No", "Yes" ) ))

#Calculate marginal effects:
ame_m1 <- avg_slopes(m1)
ame_m1$model="period 1 - no controls"
ame_m2<- avg_slopes(m2, variables = c("DCG_G_treatment"))
ame_m2$model="period 1 - with controls"
ame_m3 <- avg_slopes(m3)
ame_m3$model = "period 5 - no controls"
ame_m4<- avg_slopes(m4, variables = c("DCG_G_treatment"))
ame_m4$model = "period 5 - with controls"
ame_closeselfish <- rbind(ame_m1, ame_m2, ame_m3, ame_m4)
ame_closeselfish$term <- ifelse(ame_closeselfish$contrast=="2 - 1","T-LowThreshold","T-OnePlayer")
ame_closeselfish <- ame_closeselfish %>% select(term,estimate,std.error,p.value,model)



#Table 6


m1<-feglm(DCG_G_threshmet ~ factor(DCG_G_treatment), data=dg[dg$DCG_round_ingame==1,], family ="binomial",cluster=~UniqGroupID)
m2<-feglm(DCG_G_threshmet ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=dg[dg$DCG_round_ingame==1,], family ="binomial",cluster=~UniqGroupID)
m3<-feglm(DCG_G_threshmet ~ factor(DCG_G_treatment), data=dg[dg$DCG_round_ingame==5,], family ="binomial",cluster=~UniqGroupID)
m4<-feglm(DCG_G_threshmet ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=dg[dg$DCG_round_ingame==5,], family ="binomial",cluster=~UniqGroupID)
m5<-feols(DCG_G_threshmet ~ factor(DCG_G_treatment), data=dga,cluster=~UniqGroupID)
m6<-feols(DCG_G_threshmet ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=dga,cluster=~UniqGroupID)

etable(m1,m2,m3,m4,m5,m6, headers = c("period 1","period 1", "period 5","period 5", "average 1-5","average 1-5"), tex=TRUE, file="Tab_Paper.tex", 
       title="Treatment effects on Threshold Met", label="tab:TreatThreshmet", fitstat=c('n', 'r2', 'pr2', 'wald'), dict=dict2, order=c("T-One","!Constant"), 
       drop=c("%ipip", "%female", "%age", "%raven" , "%riskaversion_contrib"), extralines= list(Controls=c( "No", "Yes", "No", "Yes", "No", "Yes" ) ))

#Calculate marginal effects:
ame_m1 <- avg_slopes(m1)
ame_m1$model="period 1 - no controls"
ame_m2<- avg_slopes(m2, variables = c("DCG_G_treatment"))
ame_m2$model="period 1 - with controls"
ame_m3 <- avg_slopes(m3)
ame_m3$model = "period 5 - no controls"
ame_m4<- avg_slopes(m4, variables = c("DCG_G_treatment"))
ame_m4$model = "period 5 - with controls"
ame_threshmet <- rbind(ame_m1, ame_m2, ame_m3, ame_m4)
ame_threshmet$term <- ifelse(ame_threshmet$contrast=="2 - 1","T-LowThreshold","T-OnePlayer")
ame_threshmet <- ame_threshmet %>% select(term,estimate,std.error,p.value,model)



#Table 7
m1<-feglm(DCG_G_sustainable ~ factor(DCG_G_treatment), data=dg[dg$DCG_round_ingame==5,], family ="binomial",cluster=~UniqGroupID)
m2<-feglm(DCG_G_sustainable ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=dg[dg$DCG_round_ingame==5,], family ="binomial",cluster=~UniqGroupID)
m3<-feols(DCG_G_endonext ~ factor(DCG_G_treatment), data=dg[dg$DCG_round_ingame==5,], cluster=~UniqGroupID)
m4<-feols(DCG_G_endonext ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=dg[dg$DCG_round_ingame==5,],cluster=~UniqGroupID)

etable(m1,m2,m3,m4, headers = c("period 5","period 5", "period 6","period 6"), tex=TRUE, file="Tab_Paper.tex", 
       title="Treatment effects on Sustainability and Endowment", label="tab:TreatSustEndo", fitstat=c('n', 'r2', 'pr2', 'wald'), dict=dict2, order=c("T-One","!Constant"), 
       drop=c("%ipip", "%female", "%age", "%raven" , "%riskaversion_contrib"), extralines= list(Controls=c( "No", "Yes", "No", "Yes" ) ))


#Calculate marginal effects:
ame_m1 <- avg_slopes(m1)
ame_m1$model="period 5 - no controls"
ame_m2<- avg_slopes(m2, variables = c("DCG_G_treatment"))
ame_m2$model="period 5 - with controls"

ame_sustain <- rbind(ame_m1, ame_m2)
ame_sustain$term <- ifelse(ame_sustain$contrast=="2 - 1","T-LowThreshold","T-OnePlayer")
ame_sustain <- ame_sustain %>% select(term,estimate,std.error,p.value,model)


write.xlsx(ame_closecoop, file="MarginalEffects.xlsx", sheetName="CloseCoop")
write.xlsx(ame_closeselfish, file="MarginalEffects.xlsx", sheetName="CloseDefect", append=TRUE)
write.xlsx(ame_threshmet, file="MarginalEffects.xlsx", sheetName="ThreshMet", append=TRUE)
write.xlsx(ame_sustain, file="MarginalEffects.xlsx", sheetName="Sustainable", append=TRUE)





#Table 8
#Agreeableness

m1<-feols(contrib_share ~ factor(DCG_G_treatment)+ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=d[d$DCG_round_ingame==1,], cluster=~UniqGroupID)
m2<-feols(contrib_share ~ factor(DCG_G_treatment)*ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=d[d$DCG_round_ingame==1,],cluster=~UniqGroupID)
m3<-feols(contrib_share ~ factor(DCG_G_treatment)+ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=d[d$DCG_round_ingame==5,], cluster=~UniqGroupID)
m4<-feols(contrib_share ~ factor(DCG_G_treatment)*ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=d[d$DCG_round_ingame==5,],cluster=~UniqGroupID)
m5<-feols(contrib_share ~ factor(DCG_G_treatment)+ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=da,cluster=~UniqGroupID)
m6<-feols(contrib_share ~ factor(DCG_G_treatment)*ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=da,cluster=~UniqGroupID)

etable(m1,m2,m3,m4,m5,m6, headers = c("period 1","period 1", "period 5","period 5", "average 1-5","average 1-5"), tex=TRUE, file="Tab_Paper.tex", 
       title="Effect of Agreeableness on contribution shares", label="tab:ipipA", fitstat=c('n', 'r2',  'wald'), dict=dict2, order=c("Agree","!Constant","!T-Low"), 
       drop=c("%ipip_C", "%female", "%age", "%raven" , "%riskaversion_contrib", "T-OnePlayer$", "T-LowThreshold$"), 
       extralines= list(Controls=c( "Yes", "Yes", "Yes", "Yes", "Yes", "Yes" ) ))

#Table 9 
#Raven
m1<-feglm(close_to_eq_coop ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=d[d$DCG_round_ingame==1,], family ="binomial",cluster=~UniqGroupID)
m2<-feglm(close_to_eq_coop ~ factor(DCG_G_treatment)*raven +ipip_A + ipip_C  + female + age  + riskaversion_contrib, data=d[d$DCG_round_ingame==1,], family ="binomial",cluster=~UniqGroupID)
m3<-feglm(close_to_eq_coop ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=d[d$DCG_round_ingame==5,], family ="binomial",cluster=~UniqGroupID)
m4<-feglm(close_to_eq_coop ~ factor(DCG_G_treatment)*raven +ipip_A + ipip_C  + female + age  + riskaversion_contrib, data=d[d$DCG_round_ingame==5,], family ="binomial",cluster=~UniqGroupID)
m5<-feols(close_to_eq_coop ~ factor(DCG_G_treatment)+ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=da,cluster=~UniqGroupID)
m6<-feols(close_to_eq_coop ~ factor(DCG_G_treatment)*raven +ipip_A + ipip_C  + female + age  + riskaversion_contrib, data=da,cluster=~UniqGroupID)

etable(m1,m2,m3,m4,m5,m6, headers = c("period 1","period 1", "period 5","period 5", "average 1-5","average 1-5"), tex=TRUE,  file="Tab_Paper.tex",
       title="Effect of Raven on playing Close to Cooperation", label="tab:RavenCoop", fitstat=c('n', 'r2', 'pr2', 'wald'), dict=dict2, order=c("Raven","!Constant","!T-Low"), 
       drop=c("%ipip", "%female", "%age",  "%riskaversion_contrib", "T-OnePlayer$", "T-LowThreshold$"), 
       extralines= list(Controls=c( "Yes", "Yes", "Yes", "Yes", "Yes", "Yes" ) ))


#Table 10
#Raven

m1<-feglm(close_to_eq_selfish ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=d[d$DCG_round_ingame==1,], family ="binomial",cluster=~UniqGroupID)
m2<-feglm(close_to_eq_selfish ~ factor(DCG_G_treatment)*raven +ipip_A + ipip_C  + female + age  + riskaversion_contrib, data=d[d$DCG_round_ingame==1,], family ="binomial",cluster=~UniqGroupID)
m3<-feglm(close_to_eq_selfish ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=d[d$DCG_round_ingame==5,], family ="binomial",cluster=~UniqGroupID)
m4<-feglm(close_to_eq_selfish ~ factor(DCG_G_treatment)*raven +ipip_A + ipip_C  + female + age  + riskaversion_contrib, data=d[d$DCG_round_ingame==5,], family ="binomial",cluster=~UniqGroupID)
m5<-feols(close_to_eq_selfish ~ factor(DCG_G_treatment)+ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=da,cluster=~UniqGroupID)
m6<-feols(close_to_eq_selfish ~ factor(DCG_G_treatment)*raven +ipip_A + ipip_C  + female + age  + riskaversion_contrib, data=da,cluster=~UniqGroupID)

etable(m1,m2,m3,m4,m5,m6, headers = c("period 1","period 1", "period 5","period 5", "average 1-5","average 1-5"), tex=TRUE,  file="Tab_Paper.tex",
       title="Effect of Raven on playing Close to Cooperation", label="tab:RavenDefect", fitstat=c('n', 'r2', 'pr2', 'wald'), dict=dict2, order=c("Raven","!Constant","!T-Low"), 
       drop=c("%ipip", "%female", "%age",  "%riskaversion_contrib", "T-OnePlayer$", "T-LowThreshold$"), 
       extralines= list(Controls=c( "Yes", "Yes", "Yes", "Yes", "Yes", "Yes" ) ))




#Tables B1-B3
#Run analysis separately for repetitions 1 and 6
chosen_repet_nb=1

m1<-feglm(close_to_eq_coop ~ factor(DCG_G_treatment), data=d[d$DCG_round_ingame==1 & d$DCG_G_repet==chosen_repet_nb,], family ="binomial",cluster=~UniqGroupID)
m2<-feglm(close_to_eq_coop ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=d[d$DCG_round_ingame==1& d$DCG_G_repet==chosen_repet_nb,], family ="binomial",cluster=~UniqGroupID)
m3<-feglm(close_to_eq_coop ~ factor(DCG_G_treatment), data=d[d$DCG_round_ingame==5& d$DCG_G_repet==chosen_repet_nb,], family ="binomial",cluster=~UniqGroupID)
m4<-feglm(close_to_eq_coop ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=d[d$DCG_round_ingame==5& d$DCG_G_repet==chosen_repet_nb,], family ="binomial",cluster=~UniqGroupID)
m5<-feols(close_to_eq_coop ~ factor(DCG_G_treatment), data=da[da$DCG_G_repet==chosen_repet_nb,],cluster=~UniqGroupID)
m6<-feols(close_to_eq_coop ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=da[da$DCG_G_repet==chosen_repet_nb,],cluster=~UniqGroupID)

etable(m1,m2,m3,m4,m5,m6, headers = c("period 1","period 1", "period 5","period 5", "average 1-5","average 1-5"), tex=TRUE, file="Tab_Paper_20230708.tex", 
       title="Treatment effects on playing Close to Cooperation (repetition =1)", label="tab:TreatCoop", fitstat=c('n', 'r2', 'pr2', 'wald'), dict=dict2, order=c("T-One","!Constant"), 
       drop=c("%ipip", "%female", "%age", "%raven" , "%riskaversion_contrib"), extralines= list(Controls=c( "No", "Yes", "No", "Yes", "No", "Yes" ) ))

m1<-feglm(close_to_eq_selfish ~ factor(DCG_G_treatment), data=d[d$DCG_round_ingame==1 & d$DCG_G_repet==chosen_repet_nb,], family ="binomial",cluster=~UniqGroupID)
m2<-feglm(close_to_eq_selfish ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=d[d$DCG_round_ingame==1 & d$DCG_G_repet==chosen_repet_nb,], family ="binomial",cluster=~UniqGroupID)
m3<-feglm(close_to_eq_selfish ~ factor(DCG_G_treatment), data=d[d$DCG_round_ingame==5 & d$DCG_G_repet==chosen_repet_nb,], family ="binomial",cluster=~UniqGroupID)
m4<-feglm(close_to_eq_selfish ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=d[d$DCG_round_ingame==5 & d$DCG_G_repet==chosen_repet_nb,], family ="binomial",cluster=~UniqGroupID)
m5<-feols(close_to_eq_selfish ~ factor(DCG_G_treatment), data=da[da$DCG_G_repet==chosen_repet_nb,],cluster=~UniqGroupID)
m6<-feols(close_to_eq_selfish ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=da[da$DCG_G_repet==chosen_repet_nb,],cluster=~UniqGroupID)

etable(m1,m2,m3,m4,m5,m6, headers = c("period 1","period 1", "period 5","period 5", "average 1-5","average 1-5"), tex=TRUE, file="Tab_Paper_20230708.tex", 
       title="Treatment effects on playing Close to Defection (repetition =1)", label="tab:TreatDefect", fitstat=c('n', 'r2', 'pr2', 'wald'), dict=dict2, order=c("T-One","!Constant"), 
       drop=c("%ipip", "%female", "%age", "%raven" , "%riskaversion_contrib"), extralines= list(Controls=c( "No", "Yes", "No", "Yes", "No", "Yes" ) ))

m1<-feglm(DCG_G_threshmet ~ factor(DCG_G_treatment), data=dg[dg$DCG_round_ingame==1 & dg$DCG_G_repet==chosen_repet_nb,], family ="binomial",cluster=~UniqGroupID)
m2<-feglm(DCG_G_threshmet ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=dg[dg$DCG_round_ingame==1 & dg$DCG_G_repet==chosen_repet_nb,], family ="binomial",cluster=~UniqGroupID)
m3<-feglm(DCG_G_threshmet ~ factor(DCG_G_treatment), data=dg[dg$DCG_round_ingame==5 & dg$DCG_G_repet==chosen_repet_nb,], family ="binomial",cluster=~UniqGroupID)
m4<-feglm(DCG_G_threshmet ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=dg[dg$DCG_round_ingame==5 & dg$DCG_G_repet==chosen_repet_nb,], family ="binomial",cluster=~UniqGroupID)
m5<-feols(DCG_G_threshmet ~ factor(DCG_G_treatment), data=dga[dga$DCG_G_repet==chosen_repet_nb,],cluster=~UniqGroupID)
m6<-feols(DCG_G_threshmet ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=dga[dga$DCG_G_repet==chosen_repet_nb,],cluster=~UniqGroupID)

etable(m1,m2,m3,m4,m5,m6, headers = c("period 1","period 1", "period 5","period 5", "average 1-5","average 1-5"), tex=TRUE, file="Tab_Paper_20230708.tex", 
       title="Treatment effects on Threshold Met (repetition =1)", label="tab:TreatThreshmet", fitstat=c('n', 'r2', 'pr2', 'wald'), dict=dict2, order=c("T-One","!Constant"), 
       drop=c("%ipip", "%female", "%age", "%raven" , "%riskaversion_contrib"), extralines= list(Controls=c( "No", "Yes", "No", "Yes", "No", "Yes" ) ))


#Table B4

m1<-feols(DCG_G_endowment ~ factor(DCG_G_treatment)*DCG_round_ingame, data=dg, cluster=~UniqGroupID)
m2<-feols(DCG_G_endowment ~ factor(DCG_G_treatment)*DCG_round_ingame +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=dg, cluster=~UniqGroupID)
etable(m1,m2,tex=TRUE,  
       title="Treatment effects and endowment time trend", label="tab:TreatTimeTrend", fitstat=c('n', 'r2', 'pr2', 'wald'), dict=dict2, file="Tab_Paper.tex", 
       drop=c("%ipip", "%female", "%age", "%raven" , "%riskaversion_contrib"),        order=c("Period","!Constant","!Thresh" ),      
       extralines= list(Controls=c( "No", "Yes" ) )  )



#table B6
m1<-feglm(DCG_P_overcontribdum ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=d[d$DCG_round_ingame==1,], family ="binomial",cluster=~UniqGroupID)
m2<-feglm(DCG_P_overcontribdum ~ factor(DCG_G_treatment)*raven +ipip_A + ipip_C  + female + age  + riskaversion_contrib, data=d[d$DCG_round_ingame==1,], family ="binomial",cluster=~UniqGroupID)
m3<-feglm(DCG_P_overcontribdum ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=d[d$DCG_round_ingame==5,], family ="binomial",cluster=~UniqGroupID)
m4<-feglm(DCG_P_overcontribdum ~ factor(DCG_G_treatment)*raven +ipip_A + ipip_C  + female + age  + riskaversion_contrib, data=d[d$DCG_round_ingame==5,], family ="binomial",cluster=~UniqGroupID)
m5<-feols(DCG_P_overcontribdum ~ factor(DCG_G_treatment)+ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=da,cluster=~UniqGroupID)
m6<-feols(DCG_P_overcontribdum ~ factor(DCG_G_treatment)*raven +ipip_A + ipip_C  + female + age  + riskaversion_contrib, data=da,cluster=~UniqGroupID)

etable(m1,m2,m3,m4,m5,m6, headers = c("period 1","period 1", "period 5","period 5", "average 1-5","average 1-5"), tex=TRUE, 
       title="Effect of Raven on Overcontribution dummy", label="tab:RavenOverContrDum", fitstat=c('n', 'r2', 'pr2', 'wald'), dict=dict2, order=c("Raven","!Constant","!T-Low"), 
       drop=c("%ipip", "%female", "%age",  "%riskaversion_contrib", "T-OnePlayer$", "T-LowThreshold$"), 
       extralines= list(Controls=c( "Yes", "Yes", "Yes", "Yes", "Yes", "Yes" ) ))

#table B7
m1<-feglm(DCG_G_wastedum ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=dg[dg$DCG_round_ingame==1,], family ="binomial",cluster=~UniqGroupID)
m2<-feglm(DCG_G_wastedum ~ factor(DCG_G_treatment)*raven +ipip_A + ipip_C  + female + age  + riskaversion_contrib, data=dg[dg$DCG_round_ingame==1,], family ="binomial",cluster=~UniqGroupID)
m3<-feglm(DCG_G_wastedum ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=dg[dg$DCG_round_ingame==5,], family ="binomial",cluster=~UniqGroupID)
m4<-feglm(DCG_G_wastedum ~ factor(DCG_G_treatment)*raven +ipip_A + ipip_C  + female + age  + riskaversion_contrib, data=dg[dg$DCG_round_ingame==5,], family ="binomial",cluster=~UniqGroupID)
m5<-feols(DCG_G_wastedum ~ factor(DCG_G_treatment)+ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=dga,cluster=~UniqGroupID)
m6<-feols(DCG_G_wastedum ~ factor(DCG_G_treatment)*raven +ipip_A + ipip_C  + female + age  + riskaversion_contrib, data=dga,cluster=~UniqGroupID)

etable(m1,m2,m3,m4,m5,m6, headers = c("period 1","period 1", "period 5","period 5", "average 1-5","average 1-5"), tex=TRUE, 
       title="Effect of Raven on Waste dummy", label="tab:RavenWastedummy", fitstat=c('n', 'r2', 'pr2', 'wald'), dict=dict2, order=c("Raven","!Constant","!T-Low"), 
       drop=c("%ipip", "%female", "%age",  "%riskaversion_contrib", "T-OnePlayer$", "T-LowThreshold$"), 
       extralines= list(Treatmentdummies =c( "Yes", "Yes", "Yes", "Yes", "Yes", "Yes" ) ,Controls=c( "Yes", "Yes", "Yes", "Yes", "Yes", "Yes" ) ))





#Table B8 & B9


m1<-feglm(close_to_eq_coop ~ factor(DCG_G_treatment)+nep +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=d[d$DCG_round_ingame==1,], family ="binomial",cluster=~UniqGroupID)
m2<-feglm(close_to_eq_coop ~ factor(DCG_G_treatment)*nep+raven +ipip_A + ipip_C  + female + age  + riskaversion_contrib, data=d[d$DCG_round_ingame==1,], family ="binomial",cluster=~UniqGroupID)
m3<-feglm(close_to_eq_coop ~ factor(DCG_G_treatment)+nep +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=d[d$DCG_round_ingame==5,], family ="binomial",cluster=~UniqGroupID)
m4<-feglm(close_to_eq_coop ~ factor(DCG_G_treatment)*nep+raven +ipip_A + ipip_C  + female + age  + riskaversion_contrib, data=d[d$DCG_round_ingame==5,], family ="binomial",cluster=~UniqGroupID)
m5<-feols(close_to_eq_coop ~ factor(DCG_G_treatment)+nep+ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=da,cluster=~UniqGroupID)
m6<-feols(close_to_eq_coop ~ factor(DCG_G_treatment)*nep+raven +ipip_A + ipip_C  + female + age  + riskaversion_contrib, data=da,cluster=~UniqGroupID)

etable(m1,m2,m3,m4,m5,m6, headers = c("period 1","period 1", "period 5","period 5", "average 1-5","average 1-5"), tex=TRUE,  
       title="Effect of NEP on playing Close to Cooperation", label="tab:RavenCoop", fitstat=c('n', 'r2', 'pr2', 'wald'), dict=dict2, order=c("nep","!Constant","!T-Low"), 
       drop=c("%ipip", "%female","%raven", "%age",  "%riskaversion_contrib", "T-OnePlayer$", "T-LowThreshold$"), 
       extralines= list(Controls=c( "Yes", "Yes", "Yes", "Yes", "Yes", "Yes" ) ))


m1<-feglm(close_to_eq_selfish ~ factor(DCG_G_treatment)+nep +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=d[d$DCG_round_ingame==1,], family ="binomial",cluster=~UniqGroupID)
m2<-feglm(close_to_eq_selfish ~ factor(DCG_G_treatment)*nep+raven +ipip_A + ipip_C  + female + age  + riskaversion_contrib, data=d[d$DCG_round_ingame==1,], family ="binomial",cluster=~UniqGroupID)
m3<-feglm(close_to_eq_selfish ~ factor(DCG_G_treatment)+nep +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=d[d$DCG_round_ingame==5,], family ="binomial",cluster=~UniqGroupID)
m4<-feglm(close_to_eq_selfish ~ factor(DCG_G_treatment)*nep+raven +ipip_A + ipip_C  + female + age  + riskaversion_contrib, data=d[d$DCG_round_ingame==5,], family ="binomial",cluster=~UniqGroupID)
m5<-feols(close_to_eq_selfish ~ factor(DCG_G_treatment)+nep+ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=da,cluster=~UniqGroupID)
m6<-feols(close_to_eq_selfish ~ factor(DCG_G_treatment)*nep+raven +ipip_A + ipip_C  + female + age  + riskaversion_contrib, data=da,cluster=~UniqGroupID)

etable(m1,m2,m3,m4,m5,m6, headers = c("period 1","period 1", "period 5","period 5", "average 1-5","average 1-5"), tex=TRUE,  
       title="Effect of NEP on playing Close to Defection", label="tab:RavenCoop", fitstat=c('n', 'r2', 'pr2', 'wald'), dict=dict2, order=c("nep","!Constant","!T-Low"), 
       drop=c("%ipip", "%female","%raven", "%age",  "%riskaversion_contrib", "T-OnePlayer$", "T-LowThreshold$"), 
       extralines= list(Controls=c( "Yes", "Yes", "Yes", "Yes", "Yes", "Yes" ) ))

#Table B5

m1<-feols(contrib_share ~ factor(DCG_G_treatment), data=d[d$DCG_round_ingame==1,], cluster=~UniqGroupID)
m2<-feols(contrib_share ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=d[d$DCG_round_ingame==1,],cluster=~UniqGroupID)
m3<-feols(contrib_share ~ factor(DCG_G_treatment), data=d[d$DCG_round_ingame==5,], cluster=~UniqGroupID)
m4<-feols(contrib_share ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=d[d$DCG_round_ingame==5,], cluster=~UniqGroupID)
m5<-feols(contrib_share ~ factor(DCG_G_treatment), data=da,cluster=~UniqGroupID)
m6<-feols(contrib_share ~ factor(DCG_G_treatment) +ipip_A + ipip_C  + female + age + raven + riskaversion_contrib, data=da,cluster=~UniqGroupID)

etable(m1,m2,m3,m4,m5,m6, headers = c("period 1","period 1", "period 5","period 5", "average 1-5","average 1-5"), tex=TRUE,  file="Tab_Paper.tex",
       title="Treatment effects on contribution shares", label="tab:TreatContrSh", fitstat=c('n', 'r2', 'pr2', 'wald'), dict=dict2, order=c("T-One","!Constant"), 
       drop=c("%ipip", "%female", "%age", "%raven" , "%riskaversion_contrib"), extralines= list(Controls=c( "No", "Yes", "No", "Yes", "No", "Yes" ) ))



